package xpn.platform.common.util;

import com.aspose.cells.License;
import com.aspose.cells.SaveFormat;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.*;

/**
 * excel 转换 pdf 工具类
 *
 * @author duwei
 * @version 2017/11/16 14:32
 */
@Slf4j
public class ExcelToPdfUtil {

    private static ExcelToPdfUtil instance = new ExcelToPdfUtil();

    private ExcelToPdfUtil() {
        try {
            InputStream is = ExcelToPdfUtil.class.getClassLoader().getResourceAsStream("\\static/aspose-cell/license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("Register excel2pdf tool failed!");
        }
    }

    public static ExcelToPdfUtil getInstance() {
        return instance;
    }

    /**
     * 根据路径转换 pdf 到 文件
     *
     * @param sourcePath      原始excel路径
     * @param destinationPath 输出路径(要带上包含后缀名 .pdf的文件名)
     */
    public void convertToFile(String sourcePath, String destinationPath) {
        try {
            long old = System.currentTimeMillis();
            log.info("Excel[{}] convert PDF[{}]", sourcePath, destinationPath);
            org.apache.poi.ss.usermodel.Workbook wb = new org.apache.poi.ss.usermodel.Workbook(sourcePath);
            File pdfFile = new File(destinationPath);
            FileOutputStream fileOS = new FileOutputStream(pdfFile);
            wb.save(fileOS, SaveFormat.PDF);
            fileOS.close();
            long now = System.currentTimeMillis();
            log.info("convert finished,elapsed time：{} seconds", ((now - old) / 1000.0));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("convert failed!", e);
        }
    }

    /**
     * 根据输入流转换 pdf 到文件
     *
     * @param inputStream     原始excel 输入流
     * @param destinationPath 输出路径(要带上包含后缀名 .pdf的文件名)
     */
    public void convertToFile(InputStream inputStream, String destinationPath) {
        File pdfFile = new File(destinationPath);
        convertToFile(inputStream, pdfFile);
    }
    /**
     * 根据输入流转换 pdf 到文件
     *
     * @param inputStream     原始excel 输入流
     * @param destinationFile 输出文件
     */
    public void convertToFile(InputStream inputStream, File destinationFile) {
        try {
            long old = System.currentTimeMillis();
            log.info("Excel convert PDF[{}]", destinationFile);
            org.apache.poi.ss.usermodel.Workbook wb = new org.apache.poi.ss.usermodel.Workbook(inputStream);
            saveToFile(destinationFile, wb);
            inputStream.close();
            long now = System.currentTimeMillis();
            log.info("convert finished,elapsed time：{} seconds", ((now - old) / 1000.0));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("convert failed!", e);
        }
    }


    /**
     * 根据输出流转换 pdf 到文件
     *
     * @param outputStream    原始excel 输出
     * @param destinationPath 输出路径(要带上包含后缀名 .pdf的文件名)
     */
    public void convertToFile(OutputStream outputStream, String destinationPath) {
        File pdfFile = new File(destinationPath);
        convertToFile(outputStream, pdfFile);

    }

    /**
     * 根据输出流转换 pdf 到文件
     *
     * @param outputStream    原始excel 输出
     * @param destinationFile 输出文件
     */
    public void convertToFile(OutputStream outputStream, File destinationFile) {
        try {
            InputStream inputStream = outputStreamToInputStream(outputStream);
            convertToFile(inputStream, destinationFile);
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("convert failed!", e);
        }
    }

    /**
     * 保存pdf到文件
     *
     * @param destinationFile
     * @param wb
     * @throws Exception
     */
    private void saveToFile(File destinationFile, org.apache.poi.ss.usermodel.Workbook wb) throws Exception {
        FileOutputStream fileOS = new FileOutputStream(destinationFile);
        wb.save(fileOS, SaveFormat.PDF);
        fileOS.close();
    }


    /**
     * 根据输入流转换 pdf 到 输出流
     *
     * @param fileOS        原始excel 输出流
     * @param destinationOS 转换完成后的输出流,需要自己关闭
     */
    public void convertToStream(OutputStream fileOS, OutputStream destinationOS) {
        try {
            InputStream inputStream = outputStreamToInputStream(fileOS);
            org.apache.poi.ss.usermodel.Workbook wb = new Workbook(inputStream);
            wb.save(destinationOS, SaveFormat.PDF);
            fileOS.close();
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("convert failed!", e);
        }
    }

    /**
     * 输出流转输入流  避免产生文件
     *
     * @param outputStream
     * @return
     * @throws Exception
     */
    private InputStream outputStreamToInputStream(OutputStream outputStream) throws Exception {
        ByteArrayOutputStream byteArrayOutputStream;
        byteArrayOutputStream = (ByteArrayOutputStream) outputStream;
        return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    }
}